var opcoes_maps = {
	draggable : true
};

var MostraDirecao;
// var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
var directionService = new google.maps.DirectionsService();
var map;
var geocoder;



function initialize() {

	MostraDirecao = new google.maps.DirectionsRenderer(opcoes_maps);
	// centralizando mapa
	var mapCenter = new google.maps.LatLng(-23.559432267613275, -46.61322677182613);

	var mapOptions = {
		zoom : 12,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		center : mapCenter
	}

	map = new google.maps.Map(document.getElementById("mapa"), mapOptions);

	MostraDirecao.setMap(map);
	MostraDirecao.setPanel(document.getElementById("caminho"));

	var defaultBounds = new google.maps.LatLngBounds(new google.maps.LatLng(
			-22.547817, -44.171219));

	var input_origem = document.getElementById('origem');
	var options = {
		bounds : defaultBounds,
		types : [ 'geocode' ]
	};

	autocomplete1 = new google.maps.places.Autocomplete(input_origem, options);

	var input_destino = document.getElementById('destino');
	var options = {
		bounds : defaultBounds,
		types : [ 'geocode' ]
	};

	autocomplete2 = new google.maps.places.Autocomplete(input_destino, options);
}

// transformando o endere�o buscado em coordenadas
function codeAddress() {
	var origem = document.getElementById('origem').value;
	var destino = document.getElementById('destino').value;
	
	geocoder = new google.maps.Geocoder();
	
	//obtem a latlng da origem
	geocoder.geocode({'address' : origem}, 
	function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			/*map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
				map : map,
				position : results[0].geometry.location
			});*/
			 
			document.getElementById('origemLat').value = results[0].geometry.location.lat();
			document.getElementById('origemLon').value = results[0].geometry.location.lng();
//			alert("Origem: " + results[0].geometry.location.lat());
			
			
		} else {
//			alert("Geocode was not successful for the following reason: "
//					+ status);
		}
	});
	
	geocoder.geocode({'address' : destino }, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			/*map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
				map : map,
				position : results[0].geometry.location
			});*/
			document.getElementById('destinoLat').value = results[0].geometry.location.lat();
			document.getElementById('destinoLon').value = results[0].geometry.location.lng();
//			alert("Destino: " + results[0].geometry.location);
			
		} else {
//			alert("Geocode was not successful for the following reason: "
//					+ status);
		}
	});
}
var shape_lat = [-23.585012,
                 -23.585043,
                 -23.585047,
                 -23.584954,
                 -23.584859,
                 -23.58479,
                 -23.584823,
                 -23.584855,
                 -23.585,
                 -23.58515,
                 -23.5852,
                 -23.584362
];

var shape_long = [-46.524466,
                  -46.524976,
                  -46.525013,
                  -46.525042,
                  -46.525238,
                  -46.525523,
                  -46.525595,
                  -46.525668,
                  -46.525985,
                  -46.526105,
                  -46.52692,
                  -46.528113
];

// calculando rota
function calculaRota() {
	
//	var rota = new Array();
//	rota = request.getAttribute("rotas");
//	alert(rota.length);
	var origem = document.getElementById("origem").value;
	var destino = document.getElementById("destino").value;
	var i;
	var coordenada = new Array(); 
	coordenada = [];
	//recentralizando o mapa com a primeira posi��o do array
 	var mapCenter = new google.maps.LatLng(shape_lat[0],shape_long[0]);
  	var secMapOptions = {
  			zoom : 18,
  			mapTypeId : google.maps.MapTypeId.TRANSIT,
  			center : mapCenter
  		};

  		map = new google.maps.Map(document.getElementById("mapa"), secMapOptions);
  		
  		
			for(i=0;i<shape_lat.length;i++)
			{
				if(coordenada[i]==null)
				{
					coordenada[i] = new google.maps.LatLng(shape_lat[i],shape_long[i]);
				}
				else
					coordenada[i] = coordenada[i-1] +"," + new google.maps.LatLng(shape_lat[i],shape_long[i]);
				
			}
			alert(coordenada);
  
//	          	  var coordenadas2 = [
//	          	           	      new google.maps.LatLng(45.772323, -136.214897),
//	          	           	      new google.maps.LatLng(32.291982, -175.821856),
//	          	           	      new google.maps.LatLng(-2.142599, 195.431),
//	          	           	      new google.maps.LatLng(-25.46758, 145.027892)
//	          	           	  ];

	          	  var montaCaminho = new google.maps.Polyline({
	          	    path: coordenada,// recebe o array de coordenadas
	          	    strokeColor: '#FF0000',//cor
	          	    strokeOpacity: 1.0,//opacidade
	          	    strokeWeight: 3 //espessura da linha
	          	  });
	     
	          	  montaCaminho.setMap(map);
	          	  
	          	document.getElementById('submitId').value = "1";
	          	
	/*
	 * var coordenadas = [ origem, destino new google.maps.LatLng(37.772323,
	 * -122.214897), new google.maps.LatLng(21.291982, -157.821856), new
	 * google.maps.LatLng(-18.142599, 178.431), new
	 * google.maps.LatLng(-27.46758, 153.027892) ];
	 */

	/*
	 * var montaCaminho = new google.maps.Polyline({ path: coordenadas,// recebe
	 * o array de coordenadas strokeColor: '#FF0000',//cor strokeOpacity:
	 * 1.0,//opacidade strokeWeight: 3 //espessura da linha });
	 * 
	 * montaCaminho.setMap(map);
	 */

	// exibe a distancia!
	/*
	 * function calculaDistancias() { var origin2 = 'Greenwich, England'; var
	 * destinationA = 'Stockholm, Sweden'; var service = new
	 * google.maps.DistanceMatrixService(); service.getDistanceMatrix( {
	 * origins: [origin2], destinations: [destinationA], travelMode:
	 * google.maps.TravelMode.DRIVING, unitSystem:
	 * google.maps.UnitSystem.METRIC, avoidHighways: false, avoidTolls: false },
	 * callback); }
	 */

	// function callback(response, status) {
	// if (status != google.maps.DistanceMatrixStatus.OK) {
	// alert('Error was: ' + status);
	// } else {
	// var origins = response.originAddresses;
	// var destinations = response.destinationAddresses;
	// var outputDiv = document.getElementById("outputDiv");
	// outputDiv.innerHTML = '';
	// deleteOverlays();
	//
	// for (var i = 0; i < origins.length; i++) {
	// var results = response.rows[i].elements;
	// print("ORIGEM:",origins[i]);
	// // addMarker(origins[i], false);
	// for (var j = 0; j < results.length; j++) {
	// print("destino:",destinations[j]+" dura��o"+results[j].distance.text);
	// // addMarker(destinations[j], true);
	// // outputDiv.innerHTML += origins[i] + ' to ' + destinations[j]
	// // + ': ' + results[j].distance.text + ' in '
	// // + results[j].duration.text + '<br>';
	// }
	// }
	// }
	// }
//	googlo direction e polylines automaticas abaixo
	
	/*
	 * directionService.route(request, function(result, status) { if (status ==
	 * google.maps.DirectionsStatus.OK) { MostraDirecao.setDirections(result); }
	 * });
	 */
}
function submitForm(){
document.forms['formId'].submit();
return false;
}